<?php

/**
 * empresa actions.
 *
 * @package    Acatec
 * @subpackage empresa
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class empresaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->crm_empresas = Doctrine_Core::getTable('Empresa')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->crm_empresa = Doctrine_Core::getTable('Empresa')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->crm_empresa);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new EmpresaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new EmpresaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($crm_empresa = Doctrine_Core::getTable('Empresa')->find(array($request->getParameter('id'))), sprintf('Object crm_empresa does not exist (%s).', $request->getParameter('id')));
    $this->form = new EmpresaForm($crm_empresa);
    
    // Y creo una vble de sesión para que en la empresa al actualizar no embeba el formulario si viene de aquí.
    $this->getUser()->setAttribute('confgeneral', false);
    $this->getUser()->setAttribute('confcumples', false);
    
    //Deshabilitar los elementos del formulario embebido.
    //unset($this->embeddedForms['CrmConfiguracion'][0]);
    //unset($this->validatorSchema['CrmConfiguracion'][0]);
    //$this->form->getEmbeddedForm('Crm')->disable();
    //$this->form->setWidget('campo', new sfWidgetFormInputHidden(array()));
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($crm_empresa = Doctrine_Core::getTable('Empresa')->find(array($request->getParameter('id'))), sprintf('Object crm_empresa does not exist (%s).', $request->getParameter('id')));
    $this->form = new EmpresaForm($crm_empresa);
    
    // Embebemos las relaciones solo si estamos editando la configuración general.
    if ($this->getUser()->getAttribute('confgeneral')) {
        $this->form->embedRelation('Configuracion');
    }
    // Embebemos las relaciones solo si estamos editando los cumpleaños.
    if ($this->getUser()->getAttribute('confcumples')) {
        $this->form->embedRelation('Cumpleanyos');
    }
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($crm_empresa = Doctrine_Core::getTable('Empresa')->find(array($request->getParameter('id'))), sprintf('Object crm_empresa does not exist (%s).', $request->getParameter('id')));
    $crm_empresa->delete();

    $this->redirect('empresa/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $crm_empresa = $form->save();
      
      $this->getUser()->setFlash('notice', sfContext::getInstance()->getI18n()->__('La configuración ha sido guardada'));
      
      $this->redirect('inicio/list');      
    }
  }
}
